import Vue from 'vue'
import VueRouter from 'vue-router'
import Reg from '@/views/Reg/Reg'
import Login from '@/views/Login/Login'
import Main from '@/views/Main/Main'
import store from '@/store/index'
// 二级路由
import Home from '@/views/Menus/Home/Home.vue'
import UserInfo from '@/views/Menus/User/UserInfo'
import UserAvatar from '@/views/Menus/User/UserAvatar'
import UserPwd from '@/views/Menus/User/UserPwd'
import ArtCate from '@/views/Menus/Article/ArtCate'
import ArtList from '@/views/Menus/Article/ArtList'
Vue.use(VueRouter)

const routes = [
  { path: '/Reg', component: Reg },
  { path: '/Login', component: Login },
  {
    path: '/',
    component: Main,
    redirect: '/home',
    children: [
      { path: 'home', component: Home },
      { path: 'user-info', component: UserInfo },
      { path: 'user-avatar', component: UserAvatar },
      { path: 'user-pwd', component: UserPwd },
      { path: 'art-cate', component: ArtCate },
      { path: 'art-list', component: ArtList }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 设置全局前置守卫
router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  if (!token && to.path !== '/login' && to.path !== '/reg') {
    next('/login')
  } else {
    next()
  }
})
export default router
